package com.zgh.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.zgh.domain.*;
import com.zgh.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private NewsService newsService;

    @RequestMapping(value = "/getAllNews/{provinceName}",method = RequestMethod.GET)
    public Result getAllNews(@PathVariable String provinceName){
        List<News> newsList = newsService.findAllNews(provinceName);
        return new Result(true,"查询成功",newsList);
    }

    @RequestMapping(value = "/getNewById/{id}",method = RequestMethod.GET)
    public Result getNewsById(@PathVariable String id){
        News news = newsService.findNewsById(id);
        return new Result(true,"查询成功",news);
    }

    @RequestMapping(value = "/getNews/{provinceName}",method = RequestMethod.GET)
    public Result getNews(@PathVariable String provinceName){
        List<News> newsList = newsService.findNews(provinceName);
        return new Result(true,"查询成功",newsList);
    }

    @RequestMapping(value = "/findNewsByCondition/{condition}/{region}",method = RequestMethod.GET)
    public Result findNewsByCondition(@PathVariable String condition,@PathVariable String region){
        List<News> newsList = newsService.findNewsByCondition(condition,region);
        return new Result(true,"查询成功",newsList);
    }

    /**
     * 上传新闻
     * @param
     * @return 失败或成功
     */
    @RequestMapping(value = "/uploadNews",method = RequestMethod.POST)
    public Result uploadBbs(@RequestBody String news){
        News news1 = new News();
        news1.setTitle(JSON.parseObject(news).getString("title"));
        news1.setContent(JSON.parseObject(news).getString("content"));
        news1.setProvinceName(JSON.parseObject(news).getString("province"));
        news1.setUserId(JSON.parseObject(news).getInteger("userId"));
        User user1 = new User();
        user1.setId(JSON.parseObject(news).getInteger("userId"));
        news1.setUser(user1);
        int pd =  newsService.uploadNews(news1);
        if (pd==1){
            return new Result(true,"发布成功",null);
        } else {
            return new Result(false,"发布失败",null);
        }
    }

    // 分页查询
    @RequestMapping(value = "/getAllNewsInfo",method = RequestMethod.POST)
    public Result getAllNewsInfo(@RequestBody String condition){
        //System.out.println(JSON.parseObject(condition));
        String query = JSON.parseObject(condition).getString("query");
        int pagenum = JSON.parseObject(condition).getInteger("pagenum");
        int pagesize = JSON.parseObject(condition).getInteger("pagesize");
        //System.out.println(pagesize);

        ipageNews ipageNews = newsService.getAllNewsInfo(pagenum,pagesize,query);
        //System.out.println(ipageUser);
        if (ipageNews!=null){
            return new Result(true,"获取新闻列表成功",ipageNews);
        }else {
            return new Result(false, "获取新闻列表失败", null);
        }
    }


    // 通过id获取新闻信息
    @RequestMapping(value = "/getInfoByid",method = RequestMethod.POST)
    public Result getUserInfoByid(@RequestBody String idStr){
        Integer id = JSON.parseObject(idStr).getInteger("id");
        News news = newsService.getNewsById(id);
        if (news!=null){
            return new Result(true,"获取成功",news);
        }else {
            return new Result(false, "获取失败", null);
        }
    }

    // 通过id修改博客信息
    @RequestMapping(value = "/saveInfoByid",method = RequestMethod.POST)
    public Result saveInfoByid(@RequestBody String news){
        News news1 = new News();
        news1.setTitle(JSON.parseObject(news).getString("title"));
        news1.setId(JSON.parseObject(news).getInteger("id"));
        int count = newsService.saveInfoByid(news1);
        if (count==1){
            return new Result(true,"保存博客信息成功",null);
        }else {
            return new Result(false, "保存博客信息失败", null);
        }
    }
    // 通过id 删除新闻信息
    @RequestMapping(value = "/deleteByid",method = RequestMethod.POST)
    public Result deleteByid(@RequestBody String idStr){
        Integer id = JSON.parseObject(idStr).getInteger("id");
        int count = newsService.deleteByid(id);
        if (count!=0){
            return new Result(true,"删除成功",null);
        }else {
            return new Result(false, "删除失败", null);
        }
    }


    // 通过点赞量和搜索词获取新闻
    @RequestMapping(value = "/getNewsByValue",method = RequestMethod.POST)
    public Result getBlogByValue(@RequestBody String value){
        String value1 = JSON.parseObject(value).getString("value");
        System.out.println(value1);
        List<News> newsList = newsService.getNewsByValue(value1);
        if (newsList!=null){
            return new Result(true,"获取新闻信息成功",newsList);
        }else {
            return new Result(false, "保存新闻信息失败", null);
        }
    }
}
